* Use shootings as an instrument for changes to gun policy.
* Appendix J

clear all
set more off
set seed 8675309

include "bill-analysis/bill_setup.do"  // load data and setup variables
run "_reg_labels.do"  // simplify variable labels for output

// set range to 1991 to 2014 because equation is in first differences
loc rng "if inrange(year, 1991, 2014)"

gen double firearm_death_rate = /*
    */ (all_gun_hom + all_gun_sui + all_gun_acc + all_gun_unk) / (pop / 100000)
la var firearm_death_rate "Firearm deaths (excluding war) per 100,000 people"

// 5-year average of cook index
gen int end = year
merge 1:1 fips end using "`out'/cook.dta", keep(1 3) nogen keepusing(mucook)
la var mucook "Five-year rolling mean percent of suicides with gun"
drop end

sort fips year

// take mass shooting fatalities out of firearm homicide rate?
gen double hom_rate = (all_gun_hom / (pop / 100000)) - pc_`fatal'
gen double sui_rate = (all_gun_sui / (pop / 100000))
replace firearm_death_rate = firearm_death_rate - pc_`fatal'

gen double delta_gun_control = laws_tighten - laws_loosen
gen double ln_fir = log(firearm_death_rate)
gen double ln_hom = log(hom_rate)
gen double ln_sui = log(sui_rate)
gen double ln_inc = log(inc)
gen double ln_police = log(police_emp)
gen double ln_prison = log(prisoners)
la var ln_fir "Log firearm deaths (excluding war) per 100,000"
la var ln_hom "Log firearm homicides per 100,000"
la var ln_sui "Log firearm suicides per 100,000"
la var ln_inc "Log income per capita"
la var ln_police "Log police employees per 1,000 residents"
la var ln_prison "Log prisoners per 1,000 residents"

// controls
loc ctrl "D.(ln_police ln_prison ln_inc unemp black_pct urban_pct)"
loc ctrl "`ctrl' D.(pct_0to14 pct_15to17 pct_18to24 pct_25to34)"
loc ctrl "`ctrl' D.(repleg demleg repgov)"
//loc ctrl "`ctrl' D.(mucook)"
capture drop yr*
qui tab year `rng', gen(yr)  // full set of year dummies

// political instruments
capture drop repXfatal repXshoot
capture drop demXfatal demXshoot
gen repXfatal = repleg * L.`fatal'
gen demXfatal = demleg * L.`fatal'
gen repXshoot = repleg * L.`shoot'
gen demXshoot = demleg * L.`shoot'

loc iv "L.`fatal' repXfatal"
loc est "fuller(1)"

log using "`out'/reg_iv_shooting.txt", replace
ivreg2 D.ln_fir `ctrl' yr* (delta_gun_control = `iv') `rng' /*
    */ , `est' first cluster(fips) rf nocons
rivtest, ci points(500)

// for info on rivtest command, type "net sj 9-3 st0171"
// then to install, type "net install st0171"

log close _all
exit
